﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using APMS.WebSite.Common;
using APMS.WebSite.Models.DataStore;
using APMS.WebSite.Models.ViewData.Pages;

namespace APMS.WebSite.Controllers
{
    public class CommentController : BaseController
    {
        public ActionResult Index()
        {
            // Add action logic here
            throw new NotImplementedException();
        }

        //"message",      // Route name


        public ActionResult create(int projectId, int messageId, String comment_detail)
        {
            //throw new NotImplementedException();   
            String username = "admin";
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            Comment newcomment = new Comment();
            newcomment.MessageID = messageId;
            newcomment.CommentDetail = comment_detail;
            newcomment.UserName = username;
            newcomment.ModifyDate = DateTime.Now;
            newcomment.AddDate = DateTime.Now;


            db.Comment.Add(newcomment);
            db.SubmitChanges();
            return View("test");
        }

        public ActionResult Delete(int projectId, int id)
        {
            //throw new NotImplementedException();
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();


            try
            {
                Comment deleteComment = db.Comment.Single(p => p.CommentID == id);
                db.Comment.Remove(deleteComment);
                db.SubmitChanges();
            }
            catch (InvalidOperationException ex)
            {
                System.Console.WriteLine("called on set with zero items");
            }
            ////////////////////// DELETE DATA

            return Index();

            return View("test");
        }



        public ActionResult Update(int projectId, int id)
        {
            // Initialize data required by master page

            ActionResult exception = base.InitializeAction();
            if (exception != null)
                return exception;

            // Ser Window Title
            base.SetTitle("Comment | " + id);

            // SELECT * FROM user WHERE user.UserName = '{currentUser}'
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            var query =
                from comment in db.Comment
                where
                    comment.CommentID == id
                select comment;

            // Prepare ViewData
            ManageCommentViewData viewData = new ManageCommentViewData();
            viewData.CurrentComment = query.First<Comment>();

            //TODO: Handle both POST and GET methods
            if (Request.HttpMethod == Constants.HttpMethod.POST)
            {
                // If POST means UPDATE the value from Request.Form to DB
                // If GET means VIEW the value from DB
                #region Update record back to DB
                viewData.CurrentComment.CommentDetail = Request.Form["comment_detail"];
                viewData.CurrentComment.ModifyDate = DateTime.Now;

                viewData.IsCurrentCommentUpdate = true;

                db.SubmitChanges();
                #endregion
            }
            else
            {
                viewData.IsCurrentCommentUpdate = false;
            }

            return View("Update", viewData);
        }

        public ActionResult List(int projectId, int messageId)
        {
            MySQLDataStore db = DataStoreFactory.GetMySQLDataStore();
            var query =
                from comment in db.Comment
                where
                    comment.MessageID == messageId
                select comment;

            Message message = db.Message.Single(p => p.MessageID == messageId);

            ViewData["message"] = message;
            ViewData["all"] = query.ToList();

            return View("test");
        }


    }
}
